package leecode

func detectCycle(head *ListNode) *ListNode {
	slow := head //慢指针
	fast := head //快指针
	for fast != nil && fast.Next != nil {
		//慢指针每一次移动1
		slow = slow.Next
		//快指针每一次移动2
		fast = fast.Next.Next
		//当快慢指针相遇时,说明两者都 进入了环中
		if slow == fast {
			index1 := head //index1重定向到节点头部
			index2 := fast
			//寻找环的入口
			for index1 != index2 {
				//两个指针同时走
				index1 = index1.Next
				index2 = index2.Next
			}
			return index1
		}
	}
	return nil
}
